package com.weng.cloud.service8881.mock;

import com.cloud.api.mock.MockArgDto;
import com.cloud.api.mock.MockConstants;
import com.cloud.api.mock.MockResDto;
import com.weng.cloud.commons.base.JsonUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @Author: weng
 * @Date: 2022/6/11
 * @Description: com.weng.cloud.service8881.mock
 */
@Service
public class MockRpc {

    @Resource
    private RestTemplate restTemplate;

    public List<MockResDto> rpc(List<MockArgDto> argDtos) {
        return JsonUtil.toList(this.doRpc(JsonUtil.toText(argDtos)),MockResDto.class);
    }

    private String doRpc(String arg) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity httpEntity = new HttpEntity(Collections.singletonMap("arg", arg), httpHeaders);
        return this.restTemplate.postForObject(MockConstants.URL, httpEntity, String.class);
    }
}
